package com.banyuan;


import com.banyuan.Utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

// 查询SQL语句
public class JDBCLogin {

    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String name = scanner.nextLine();
        System.out.println("请输入密码：");
        String password = scanner.nextLine();
        try {
             conn = JDBCUtils.getConnection();
             pstm = conn.prepareStatement("select count(*) c from person where username=? and password=? ");
             pstm.setObject(1,name);
            pstm.setObject(2,password);

            rs = pstm.executeQuery();
            rs.next();

            int c = rs.getInt("c");
            if (c == 1) {
                System.out.println("登陆成功");
            } else if (c == 0) {
                System.out.println("登陆失败");
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.close(rs);
            JDBCUtils.close(pstm);
            JDBCUtils.close(conn);
        }
    }

}
